package com.library.borrow.dao;

import com.framework.BaseDao;
import com.framework.Page;
import com.library.entity.Borrow;
import com.library.entity.Staff;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Repository
public class BorrowDaoImpl extends BaseDao<Borrow,String> {

	/*查询借阅条目 分页*/
	public Page<Borrow> findPageByProperty(int pageNum, int pageSize, Map paramsMap){
		int i = 0;
		int j = 0;
		String key;
		StringBuilder hql = new StringBuilder("from Borrow b");
		List<Object> params = new ArrayList();

		if(paramsMap.size() != 0){
			Iterator<String> it = paramsMap.keySet().iterator();
			hql.append(" where ");
			while (it.hasNext()) {
				key = it.next();
				params.add(paramsMap.get(key));

				if(j != 0){
					if(params.get(i) != null)
						hql.append(" and b."+ key + "=?");
					else{
						hql.append(" and b."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}
				} else{
					if(params.get(i) != null)
						hql.append(" b."+ key + "=?");
					else{
						hql.append(" b."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}

				}
				++i;
				++j;
			}
		}
		try{
			return super.findByPage(pageNum,pageSize,hql.toString(),params.toArray());
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/*查询借阅条目 不分页*/
	public List<Borrow> findBorrowsByProperty( Map paramsMap){
		int i = 0;
		int j = 0;
		String key;
		StringBuilder hql = new StringBuilder("from Borrow b");
		List<Object> params = new ArrayList();

		if(paramsMap.size() != 0){
			Iterator<String> it = paramsMap.keySet().iterator();
			hql.append(" where ");
			while (it.hasNext()) {
				key = it.next();
				params.add(paramsMap.get(key));

				if(j != 0){
					if(params.get(i) != null)
						hql.append(" and b."+ key + "=?");
					else{
						hql.append(" and b."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}
				} else{
					if(params.get(i) != null)
						hql.append(" b."+ key + "=?");
					else{
						hql.append(" b."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}

				}
				++i;
				++j;
			}
		}
		try{
			return super.findByProperty(hql.toString(),params.toArray());
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/*查找条件借阅次数*/
	public Long findCountByProperty(Map paramsMap){
		int i = 0;
		int j = 0;
		String key;
		StringBuilder hql = new StringBuilder("from Borrow b");
		List<Object> params = new ArrayList();

		if(paramsMap.size() != 0){
			Iterator<String> it = paramsMap.keySet().iterator();
			hql.append(" where ");
			while (it.hasNext()) {
				key = it.next();
				params.add(paramsMap.get(key));

				if(j != 0){
					if(params.get(i) != null)
						hql.append(" and b."+ key + "=?");
					else{
						hql.append(" and b."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}
				} else{
					if(params.get(i) != null)
						hql.append(" b."+ key + "=?");
					else{
						hql.append(" b."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}

				}
				++i;
				++j;
			}
		}
		try{
			return this.getBorrowsCount(hql.toString(),params.toArray());
		}catch(Exception e){
			e.printStackTrace();
			return (long)0;
		}
	}

	/*按条件查询数量*/
	public Long getBorrowsCount(String hql,Object[] params){
		try{
			return super.findCountByPage("select count(*) " + hql,params);
		}catch(Exception e){
			e.printStackTrace();
			return (long)0;
		}
	}

	/*查找员工当前借阅的信息 包括未取书，借阅中，归还中，已超期的书籍 不分页*/
	public List<Borrow> findCurrentBorrowsByStaff(Staff staff){
		try{
			return super.findByProperty("from Borrow bo where bo.staff=? and bo.state !=?", new Object[]{staff,3});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/*查找员工当前借阅的信息 包括未取书，借阅中，归还中，已超期的书籍 分页*/
	public Page<Borrow> findCurrentBorrowsByStaff(int pageNum,int pageSize,Staff staff){
		try{
            return super.findByPage(pageNum,pageSize,"from Borrow bo where bo.staff=? and bo.state !=?", new Object[]{staff,3});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}


	/*查找员工历史借阅的信息 指已归还的 分页*/
	public Page<Borrow> findHistoryBorrowsByStaff(int pageNum,int pageSize,Staff staff){
        try{
            return super.findByPage(pageNum,pageSize,"from Borrow bo where bo.staff=? and bo.state =?", new Object[]{staff,3});
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
	}

	/*查找员工历史借阅的信息 指已归还的 不分页*/
	public List<Borrow> findHistoryBorrowsByStaff(Staff staff){
		try{
			return super.findByProperty("from Borrow bo where bo.staff=? and bo.state =?", new Object[]{staff,3});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public List<Borrow> findCurrentBorrows(){
		try{
			return super.findByProperty("from Borrow bo where bo.state!=?", new Object[]{3});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public void saveBorrow(Borrow borrow){
		try {
			this.save(borrow);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Borrow getBorrow(int borrowId){
		try {
			Borrow bo = this.get(borrowId);
			return bo;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public void updateBorrow(Borrow bo){
		try {
			this.update(bo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List<Borrow> getAll(){
		try{
			return super.findByProperty("from Borrow", new Object[]{});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public void deleteBorrow(Borrow bo){
		try {
			this.delete(bo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
